package com.slife.serial.utils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class ByteOrderUtils {
    public static byte[] convertToBigEndian(long value) {
        byte[] bytes = ByteBuffer.allocate(8)
                .order(ByteOrder.BIG_ENDIAN)
                .putLong(value)
                .array();
        byte[] result = new byte[4];
        System.arraycopy(bytes, 4, result, 0, 4);
        return result;
    }

    public static byte[] convertToLittleEndian(long value) {
        byte[] bytes = ByteBuffer.allocate(8)
                .order(ByteOrder.LITTLE_ENDIAN)
                .putLong(value)
                .array();
        byte[] result = new byte[4];
        System.arraycopy(bytes, 0, result, 0, 4);
        return result;
    }

    public static byte[] convertToBigEndian(float value) {
        return ByteBuffer.allocate(4)
                .order(ByteOrder.BIG_ENDIAN)
                .putFloat(value)
                .array();
    }

    public static byte[] convertToLittleEndian(float value) {
        return ByteBuffer.allocate(4)
                .order(ByteOrder.LITTLE_ENDIAN)
                .putFloat(value)
                .array();
    }


}
